Lecture 7 Quiz

Question 1

A student wants to write into a file called myfile, without deleting its existing content. Which one of the following functions should he or she use?

  • f = open('myfile', 'w')
  • f = open('myfile', 'a')
  • f = open('myfile', 'r')
  • f = open('myfile', 'w+b')
'a': appendix

Question 2

Which of the following statements are true?
A) When you open a file for reading, if the file does not exist, an error occurs.
B) When you open a file for writing, if the file does not exist, a new file is created.
C) When you open a file for reading, if the file does not exist, the program will open an empty file.
D) When you open a file for writing, if the file exists, the existing file is overwritten with the new file.
E) When you open a file for writing, if the file does not exist, an error occurs.

  • A, and E
  • A, B, and D only
  • D only
  • A and B only

In [4]:
f = open('b.py', 'r')


---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-4-684c8cc425c3> in <module>()
----> 1 f = open('b.py', 'r')

IOError: [Errno 2] No such file or directory: 'b.py'

True statments:
A) When you open a file for reading, if the file does not exist, an error occurs.
B) When you open a file for writing, if the file does not exist, a new file is created.
D) When you open a file for writing, if the file exists, the existing file is overwritten with the new file.

Question 3

Examine the following three functions that take as argument a file name and return the extension of that file. For instance, if the file name is 'myfile.tar.gz' the returned value of the function should be 'gz'. If the file name has no extention, i.e. when the file name is just 'myfile', the function should return an empty string.

def get_extension1(filename):
    return(filename.split(".")[-1])


def get_extension2(filename):
    import os.path
    return(os.path.splitext(filename)[1])


def get_extension3(filename):
    return filename[filename.rfind('.'):][1:]

Which of the these functions are doing exactly what they are supposed to do according to the description above?

  • get_extension3 only
  • get_extension1 only
  • get_extension1 and get_extension2
  • All of them.

In [7]:
def get_extension1(filename):
    return(filename.split(".")[-1])


def get_extension2(filename):
    import os.path
    return(os.path.splitext(filename)[1])


def get_extension3(filename):
    return filename[filename.rfind('.'):][1:]

In [9]:
print "get_extension1"
print "get_extension1('filename'):%s" % get_extension1('filename')
print "get_extension1('filename.py'):%s" % get_extension1('filename.py')
print

print "get_extension2"
print "get_extension2('filename'):%s" % get_extension2('filename')
print "get_extension2('filename.py'):%s" % get_extension2('filename.py')
print 

print "get_extension3"
print "get_extension3('filename'):%s" % get_extension3('filename')
print "get_extension3('filename.py'):%s" % get_extension3('filename.py')


get_extension1
get_extension1('filename'):filename
get_extension1('filename.py'):py

get_extension2
get_extension2('filename'):
get_extension2('filename.py'):.py

get_extension3
get_extension3('filename'):
get_extension3('filename.py'):py

Question 4

A student is running a Python program like this:
>python mergefasta.py myfile1.fa myfile2.fa
In the mergefasta.py program the following lines are present:

import sys
tocheck=sys.argv[1]

What is the value of the variable tocheck?

  • 'myfile1.fa'
  • mergefasta.py
  • 'mergefasta.py'
  • python
  1. a.py
    # -*- coding: utf-8 -*-
    # !/usr/bin/python
    import sys
    tocheck=sys.argv[1]
    print "sys.argv[0]:", sys.argv[0]
    print "sys.argv[1]:", sys.argv[1]
    print "sys.argv[2]:", sys.argv[2]
  2. b.py
    # -*- coding: utf-8 -*-
    # !/usr/bin/python
    print 'b'
  3. c.py
    # -*- coding: utf-8 -*-
    # !/usr/bin/python
    print 'c'
    After I run python a.py b.py c.py, it shows below:
    sys.argv[0]: a.py
    sys.argv[1]: b.py
    sys.argv[2]: c.py

Question 5

A student launches the Python interpreter from his home directory. His home directory contains another directory called 'mydir'. What will happen when he write the following code at the Python prompt:

>>> import os
>>> filenames = os.listdir('mydir')
>>> f= open(filenames[0])
  • An error will be produced stating that the file to be opened does not exist.
  • A variable f representing a file object will be created, and the first file in the directory 'mydir' will be opened for reading in text mode.
  • A variable f representing a file object will be created, and the first file in the directory 'mydir' will be opened.
  • An error will be produced stating that filename is not subscriptable.

Because of current directory is not 'mydir', which just is a child directory, An error will be produced stating that the file to be opened does not exist.